// pages/card-check/card-check.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    agree:false,
    add_photo_img: [],
    money:10,
  },


  //添加图片
  chooseIMG: function (e) {
    let that = this;
    let { add_photo_img } = that.data;
    wx.chooseImage({
      count: 1,
      sizeType: ['original', 'compressed'], // 可以指定是原图还是压缩图
      sourceType: ['album', 'camera'], // 可以指定来源是相册还是相机
      success: function (res) {
        // 返回选定照片的本地文件路径列表，tempFilePaths可以作为img标签的src属性显示图片
        var tempFilePaths = res.tempFilePaths;

        wx.uploadFile({
          url: app.globalData.host + 'common/uploadimg',
          filePath: tempFilePaths[0],
          name: 'file',
          success: function (res) {
            console.log(res)
            res = JSON.parse(res.data);
            add_photo_img[0] = res.filesrc
            
            that.setData({
              add_photo_img,
            })
            console.log(that.data.add_photo_img)
          },
          fail: function (res) {
            console.log(res)
           },
          complete: function (res) { },
        })
      },
    })
  },

  // removePhoto: function (e) { //移除图片
  //   let that = this;
  //   that.setData({
  //     add_photo_img: ''
  //   })
  // },






  agreeTap: function () { //同意协议
    let that = this;
    that.data.agree = !that.data.agree;
    that.setData({
      agree: that.data.agree
    })
  },

  submitTap:function(e){ //提交
    console.log(e)
    var that = this;
    // var { agree , add_photo_img  } = that.data;
    // var formId = e.detail.formId;
    // app.getFormId(formId);
    // if (add_photo_img.length<1){
    //   wx.showToast({
    //     title:'请上传图片',
    //     icon:'none'
    //   })
    //   return;
    // }
    // if (!agree) {
    //   wx.showToast({
    //     title: '请阅读并同意用户协议',
    //     icon: 'none'
    //   })
    //   return;
    // }
    // var params = {
    //   checked:agree,
    //   image: add_photo_img[0],
    //   user_id:wx.getStorageSync('userId'),
    //   money:that.data.money
    // };
    var params = {
      user_id: wx.getStorageSync('userId'),
    }
    app.request(app.globalData.host + 'member/identification', params, 'POST', true, 
    function(res){
      console.log(res)
      if(res.data.code==1){
        // 接支付
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
        },1500)
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none'
        })
        setTimeout(function () {
            wx.navigateBack({
              delta: 1,
            })
          }, 1500)
       
      }
    },
    function(err){},
    function(complete){})
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    wx.request({
      url: app.globalData.host + 'society/getMoney',
      header: {
        'content-type': 'application/x-www-form-urlencoded'
      },
      method: "GET",
      success(res) {
        wx.hideLoading();
        console.log(res)
        that.setData({
          money:res.data.data.money
        })
      },
      fail(err) { },
      complete(comp) { },
    })
  },


  openTips(e) { //协议
    wx.navigateTo({
      url: '/pages/rule/rule?tip=3',
    })
  },


  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    var that = this;
    wx.showModal({
      title: '温馨提示',
      content: '您是否为协会会员？',
      showCancel: true,
      success: function(res) {
        if(res.confirm){
          that.submitTap();
        }else if(res.cancel){
          wx.navigateBack({
            delta: 1,
          })
        }
      },
      fail: function(res) {},
      complete: function(res) {},
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

 
})